﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace ProjetS5
{
    public enum FileNameList
    {
        Consumable, 
        Armor,
        WeaponType,
        ItemQuality,
        EquipmentSlot,
        Statistic,
        Vendor,
        PointBar,
        ObjectiveType,
        FriendshipLevel,
        CharacterRace,
        EffectSchool,
        TickType,
        ReplaceString,
        Weapon,
        EquipmentItem,
        Effect,
        Talent,
        Objective,
        Achievement,
        Ability,
        TalentTree,
        CharacterClass,
        Item,
        ItemSet,
        NonPlayableCharacter,
        NonPlayableCharacterIteration,
        QuestReward,
        Quest,
        Restriction,
        QuestQuest,
        AbilityEffects,
        AbilityFriendshipLevel,
        AbilityPointBar,
        AchievementObjectives,
        ItemAbility,
        ItemSetEffects,
        CharacterClassTalentTrees,
        CharacterClassSprites,
        ItemSetItems,
        ItemStatistic,
        NonPlayableCharacterAbility,
        NonPlayableCharacterItem,
        NonPlayableCharacterSprites,
        NonPlayableCharacterStatistic,
        QuestNonPlayableCharacterGive,
        QuestNonPlayableCharacterTurn,
        QuestObjectives,
        QuestRewardItems,
        RestrictionRestrictors,
        TalentEffects,
        TalentTreeTalents,
        ObjectiveTarget,
        VendorSell,
        CharacterClassStatistics,
        NonPlayableCharacterPointBars,
        CharacterClassPointBars,
        CharacterClassAbilities,
    }

    public class Database
    {
        public List<DatabaseTable> Datas { get; set; }
        public ContentManager Content { get; set; }
        public String DatabasePath { get; set; }

        public Database(ContentManager content)
        {
            ErrorLog.Delete("DatabaseError.txt");
            DatabasePath = "Content\\Database\\";
            Content = content;
            Datas = new List<DatabaseTable>();
            extractDatasFromFiles();
        }


        private String getNextData(FileStream file, char separator)
        {
            String data = "";
            char nextChar;

            while ((nextChar = (char)file.ReadByte()) != separator && nextChar != '\r' && nextChar != (char)(65535))
            {
                data += nextChar;
            }

            return data;
        }
        private void goToNextLine(FileStream file)
        {
            char c;
            while ((c = (char)file.ReadByte()) != '\n' && c != (char)(65535)) ; 
        }
        private bool isEndOfFile(FileStream file)
        {
            return file.Position == file.Length;
        }

        private void extractDatasFromFiles()
        {
            String errorLogName = "DatabaseError.txt";
            String[] filesName = (String[])Enum.GetNames(typeof(FileNameList));
            foreach (String fileNamee in filesName)
            {
                String fileName = DatabasePath + fileNamee + ".txt";
                if (!File.Exists(fileName))
                {
                    ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - Le fichier suivant devrait exister : " + fileName + "\n")));
                }
                else
                {
                    FileStream file = File.OpenRead(fileName);
                    extractDatasFromSingleFile(file);
                }
            }
        }
        private void extractDatasFromSingleFile(FileStream file)
        {
            String errorLogName = "DatabaseError.txt";
            String name = file.Name.Substring(file.Name.IndexOf(DatabasePath) + DatabasePath.Length - 1);
            name = name.Substring(1, name.Length - 5);
            FileNameList fnl = (FileNameList)Enum.Parse(typeof(FileNameList), name);

            goToNextLine(file);
            switch (fnl)
            {
                case FileNameList.Ability:
                    while (!isEndOfFile(file))
                    {
                        DB_Ability toAdd = new DB_Ability();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.Description = getNextData(file, '\t');
                        toAdd.CastTime = new TimeSpan(0, 0, 0, 0, Int32.Parse(getNextData(file, '\t')));
                        toAdd.Cooldown = new TimeSpan(0, 0, 0, 0, Int32.Parse(getNextData(file, '\t')));
                        toAdd.GlobalCooldown = new TimeSpan(0, 0, 0, 0, Int32.Parse(getNextData(file, '\t')));
                        toAdd.Range = Int16.Parse(getNextData(file, '\t'));
                        toAdd.Rank = Byte.Parse(getNextData(file, '\t'));
                        toAdd.RequiredLevel = Byte.Parse(getNextData(file, '\t'));
                        String icon = getNextData(file, '\t');
                        try { toAdd.Icon = Content.Load<Texture2D>(icon); }
                        catch
                        {
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - Fichier non existant : " + icon + "\n")));
                        }

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.AbilityEffects:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int abilityId = Int32.Parse(getNextData(file, '\t'));
                        DB_Ability ability = null;
                        try
                        {
                            ability = Datas.OfType<DB_Ability>().First(a => a.Identifier == abilityId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Ability - " + abilityId + "\n")));
                        }

                        int effectId = Int32.Parse(getNextData(file, '\t'));
                        DB_Effect effect = null;
                        try
                        {
                            effect = Datas.OfType<DB_Effect>().First(a => a.Identifier == effectId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Effect - " + effectId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (ability.Effects == null)
                            {
                                ability.Effects = new List<DB_Effect>();
                            }
                            ability.Effects.Add(effect);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.AbilityFriendshipLevel:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int abilityId = Int32.Parse(getNextData(file, '\t'));
                        DB_Ability ability = null;
                        try
                        {
                            ability = Datas.OfType<DB_Ability>().First(a => a.Identifier == abilityId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Ability - " + abilityId + "\n")));
                        }

                        int friendshipLevelId = Int32.Parse(getNextData(file, '\t'));
                        DB_FriendshipLevel friendshipLevel = null;
                        try
                        {
                            friendshipLevel = Datas.OfType<DB_FriendshipLevel>().First(a => a.Identifier == friendshipLevelId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Effect - " + friendshipLevelId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (ability.Target == null)
                            {
                                ability.Target = new List<DB_FriendshipLevel>();
                            }
                            ability.Target.Add(friendshipLevel);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.AbilityPointBar:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int abilityId = Int32.Parse(getNextData(file, '\t'));
                        DB_Ability ability = null;
                        try
                        {
                            ability = Datas.OfType<DB_Ability>().First(a => a.Identifier == abilityId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Ability - " + abilityId + "\n")));
                        }

                        int pointBarId = Int32.Parse(getNextData(file, '\t'));
                        DB_PointBar pointBar = null;
                        try
                        {
                            pointBar = Datas.OfType<DB_PointBar>().First(a => a.Identifier == pointBarId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : PointBar - " + pointBarId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (ability.Cost == null)
                            {
                                ability.Cost = new Dictionary<DB_PointBar,int>();
                            }
                            ability.Cost.Add(pointBar, Int32.Parse(getNextData(file, '\t')));
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Achievement:
                    while (!isEndOfFile(file))
                    {
                        DB_Achievement toAdd = new DB_Achievement();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.Description = getNextData(file, '\t');
                        toAdd.Worth = Byte.Parse(getNextData(file, '\t'));

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.AchievementObjectives:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int achievementId = Int32.Parse(getNextData(file, '\t'));
                        DB_Achievement achievement = null;
                        try
                        {
                            achievement = Datas.OfType<DB_Achievement>().First(a => a.Identifier == achievementId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Achievement - " + achievementId + "\n")));
                        }

                        int objectiveId = Int32.Parse(getNextData(file, '\t'));
                        DB_Objective objective = null;
                        try
                        {
                            objective = Datas.OfType<DB_Objective>().First(a => a.Identifier == objectiveId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Objective - " + objectiveId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (achievement.Objectives == null)
                            {
                                achievement.Objectives = new List<DB_Objective>();
                            }
                            achievement.Objectives.Add(objective);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Armor:
                    while (!isEndOfFile(file))
                    {
                        DB_Armor toAdd = new DB_Armor();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.CharacterClass:
                    while (!isEndOfFile(file))
                    {
                        DB_CharacterClass toAdd = new DB_CharacterClass();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.Description = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.CharacterClassAbilities:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int characterClassId = Int32.Parse(getNextData(file, '\t'));
                        DB_CharacterClass characterClass = null;
                        try
                        {
                            if (characterClassId != -1)
                            {
                                characterClass = Datas.OfType<DB_CharacterClass>().First(a => a.Identifier == characterClassId);
                            }
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : CharacterClass - " + characterClassId + "\n")));
                        }

                        int abilityId = Int32.Parse(getNextData(file, '\t'));
                        DB_Ability ability = null;
                        try
                        {
                            ability = Datas.OfType<DB_Ability>().First(a => a.Identifier == abilityId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Ability - " + abilityId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (characterClassId != -1)
                            {
                                if (characterClass.Abilities == null)
                                {
                                    characterClass.Abilities = new List<DB_Ability>();
                                }
                                characterClass.Abilities.Add(ability);
                            }
                            else
                            {
                                foreach (DB_CharacterClass characterClass1 in Datas.OfType<DB_CharacterClass>())
                                {
                                    if (characterClass1.Abilities == null)
                                    {
                                        characterClass1.Abilities = new List<DB_Ability>();
                                    }
                                    characterClass1.Abilities.Add(ability);
                                }
                            }
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.CharacterClassPointBars:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int characterClassId = Int32.Parse(getNextData(file, '\t'));
                        DB_CharacterClass characterClass = null;
                        try
                        {
                            characterClass = Datas.OfType<DB_CharacterClass>().First(a => a.Identifier == characterClassId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : CharacterClass - " + characterClassId + "\n")));
                        }

                        int pointBarId = Int32.Parse(getNextData(file, '\t'));
                        DB_PointBar pointBar = null;
                        try
                        {
                            pointBar = Datas.OfType<DB_PointBar>().First(a => a.Identifier == pointBarId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : PointBar - " + pointBarId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (characterClass.PointBars == null)
                            {
                                characterClass.PointBars = new List<DB_PointBar>();
                            }
                            characterClass.PointBars.Add(pointBar);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.CharacterClassSprites:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int characterClassId = Int32.Parse(getNextData(file, '\t'));
                        DB_CharacterClass characterClass = null;
                        try
                        {
                            characterClass = Datas.OfType<DB_CharacterClass>().First(a => a.Identifier == characterClassId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : CharacterClass - " + characterClassId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (characterClass.Sprites == null)
                            {
                                characterClass.Sprites = new List<Texture2D>();
                            }
                            String sprite = getNextData(file, '\t');
                            try { characterClass.Sprites.Add(Content.Load<Texture2D>(sprite)); }
                            catch
                            {
                                ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - Fichier non existant : " + sprite + "\n")));
                            }
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.CharacterClassStatistics:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int characterClassId = Int32.Parse(getNextData(file, '\t'));
                        DB_CharacterClass characterClass = null;
                        try
                        {
                            characterClass = Datas.OfType<DB_CharacterClass>().First(a => a.Identifier == characterClassId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : CharacterClass - " + characterClassId + "\n")));
                        }

                        int statisticId = Int32.Parse(getNextData(file, '\t'));
                        DB_Statistic statistic = null;
                        try
                        {
                            statistic = Datas.OfType<DB_Statistic>().First(a => a.Identifier == statisticId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Statistic - " + statisticId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (characterClass.BaseStatistics == null)
                            {
                                characterClass.BaseStatistics = new Dictionary<DB_Statistic,KeyValuePair<float,float>>();
                            }
                            float value = float.Parse(getNextData(file, '\t'));
                            float multiplier = float.Parse(getNextData(file, '\t'));
                            characterClass.BaseStatistics.Add(statistic, new KeyValuePair<float, float>(value, multiplier));
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.CharacterClassTalentTrees:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int characterClassId = Int32.Parse(getNextData(file, '\t'));
                        DB_CharacterClass characterClass = null;
                        try
                        {
                            characterClass = Datas.OfType<DB_CharacterClass>().First(a => a.Identifier == characterClassId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : CharacterClass - " + characterClassId + "\n")));
                        }

                        int talentTreeId = Int32.Parse(getNextData(file, '\t'));
                        DB_TalentTree talentTree = null;
                        try
                        {
                            talentTree = Datas.OfType<DB_TalentTree>().First(a => a.Identifier == talentTreeId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : TalentTree - " + talentTreeId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (characterClass.TalentTrees == null)
                            {
                                characterClass.TalentTrees = new List<DB_TalentTree>();
                            }
                            characterClass.TalentTrees.Add(talentTree);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.CharacterRace:
                    while (!isEndOfFile(file))
                    {
                        DB_CharacterRace toAdd = new DB_CharacterRace();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.Description = getNextData(file, '\t');
                        toAdd.IsPlayable = (Int32.Parse(getNextData(file, '\t')) == 1 ? true : false);

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Consumable:
                    while (!isEndOfFile(file))
                    {
                        DB_Consumable toAdd = new DB_Consumable();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Cooldown = new TimeSpan(0, 0, 0, 0, Int32.Parse(getNextData(file, '\t')));
                        toAdd.SharedCooldownGroup = Int32.Parse(getNextData(file, '\t'));

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Effect:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;
                        int min;
                        int max;

                        DB_Effect toAdd = new DB_Effect();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));

                        int statisticId = Int32.Parse(getNextData(file, '\t'));
                        DB_Statistic statistic = null;
                        try
                        {
                            statistic = Datas.OfType<DB_Statistic>().First(a => a.Identifier == statisticId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Statistic - " + statisticId + "\n")));
                        }
                        min = Int32.Parse(getNextData(file, '\t'));
                        max = Int32.Parse(getNextData(file, '\t'));
                        if (canOperate)
                        {
                            toAdd.Affect = new KeyValuePair<DB_Statistic, int[]>(statistic, new int[2]{min, max});
                        }

                        toAdd.Duration = new TimeSpan(0, 0, 0, 0, Int32.Parse(getNextData(file, '\t')));
                        toAdd.IsBenefical = (Int32.Parse(getNextData(file, '\t')) == 1 ? true : false);
                        toAdd.NumberOfTicks = Int32.Parse(getNextData(file, '\t'));
                        toAdd.IsVisible = (Int32.Parse(getNextData(file, '\t')) == 1 ? true : false);

                        canOperate = true;
                        int effectSchoolId = Int32.Parse(getNextData(file, '\t'));
                        DB_EffectSchool effectSchool = null;
                        try
                        {
                            effectSchool = Datas.OfType<DB_EffectSchool>().First(a => a.Identifier == effectSchoolId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : EffectSchool - " + effectSchoolId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.School = effectSchool;
                        }

                        canOperate = true;
                        int tickTypeId = Int32.Parse(getNextData(file, '\t'));
                        DB_TickType tickType = null;
                        try
                        {
                            tickType = Datas.OfType<DB_TickType>().First(a => a.Identifier == tickTypeId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : TickType - " + tickTypeId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.TickType = tickType;
                        }
                        String icon = getNextData(file, '\t');
                        try { toAdd.Icon = Content.Load<Texture2D>(icon); }
                        catch
                        {
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - Fichier non existant : " + icon + "\n")));
                        }

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.EffectSchool:
                    while (!isEndOfFile(file))
                    {
                        DB_EffectSchool toAdd = new DB_EffectSchool();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.School = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.EquipmentItem:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;
                        bool isNew = false;

                        // Check existence
                        int equipmentItemId = Int32.Parse(getNextData(file, '\t'));
                        DB_EquipmentItem toAdd = null;
                        try
                        {
                            if ((toAdd = this.Datas.OfType<DB_Armor>().First<DB_Armor>(c => c.Identifier == equipmentItemId)) != null) { }
                            else if ((toAdd = this.Datas.OfType<DB_Weapon>().First<DB_Weapon>(c => c.Identifier == equipmentItemId)) != null) { }
                        }
                        catch { }
                        // Checked

                        if (toAdd == null)
                        {
                            isNew = true;
                            toAdd = new DB_EquipmentItem();
                        }

                        toAdd.Identifier = equipmentItemId;
                        toAdd.Durability = Int32.Parse(getNextData(file, '\t'));

                        int equipmentSlotId = Int32.Parse(getNextData(file, '\t'));
                        DB_EquipmentSlot equipmentSlot = null;
                        try
                        {
                            equipmentSlot = Datas.OfType<DB_EquipmentSlot>().First(a => a.Identifier == equipmentSlotId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : EquipmentSlot - " + equipmentSlotId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.EquipmentSlot = equipmentSlot;
                        }

                        if (isNew)
                        {
                            Datas.Add(toAdd);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.EquipmentSlot:
                    while (!isEndOfFile(file))
                    {
                        DB_EquipmentSlot toAdd = new DB_EquipmentSlot();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.FriendshipLevel:
                    while (!isEndOfFile(file))
                    {
                        DB_FriendshipLevel toAdd = new DB_FriendshipLevel();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.Description = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Item:
                    while (!isEndOfFile(file))
                    {
                        bool isNew = false;
                        // Check existence
                        int itemId = Int32.Parse(getNextData(file, '\t'));
                        DB_Item toAdd = null;
                        try
                        {
                            if ((toAdd = this.Datas.OfType<DB_Armor>().First<DB_Armor>(c => c.Identifier == itemId)) != null) { }
                            else if ((toAdd = this.Datas.OfType<DB_Consumable>().First<DB_Consumable>(c => c.Identifier == itemId)) != null) { }
                            else if ((toAdd = this.Datas.OfType<DB_Weapon>().First<DB_Weapon>(c => c.Identifier == itemId)) != null) { }
                            else if ((toAdd = this.Datas.OfType<DB_EquipmentItem>().First<DB_EquipmentItem>(c => c.Identifier == itemId)) != null) { }
                        }
                        catch { }
                        // Checked

                        if (toAdd == null)
                        {
                            toAdd = new DB_Item();
                            isNew = true;
                        }
                        toAdd.Identifier = itemId;
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.MaxStack = Int32.Parse(getNextData(file, '\t'));
                        toAdd.BaseRarity = float.Parse(getNextData(file, '\t'));
                        String icon = getNextData(file, '\t');
                        try { toAdd.Icon = Content.Load<Texture2D>(icon); }
                        catch
                        {
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - Fichier non existant : " + icon + "\n")));
                        }
                        toAdd.IsGenericDrop = (Int32.Parse(getNextData(file, '\t')) == 1 ? true : false);
                        toAdd.ItemLevel = Int32.Parse(getNextData(file, '\t'));
                        toAdd.RequiredLevel = Int32.Parse(getNextData(file, '\t'));
                        toAdd.SellPrice = Int32.Parse(getNextData(file, '\t'));

                        bool canOperate = true;
                        int qualityId = Int32.Parse(getNextData(file, '\t'));
                        DB_ItemQuality quality = null;
                        try
                        {
                            quality = Datas.OfType<DB_ItemQuality>().First(s => s.Identifier == qualityId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : ItemQuality - " + qualityId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.Quality = quality;
                        }

                        if (isNew)
                        {
                            Datas.Add(toAdd);
                        }
                        
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ItemAbility:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int itemId = Int32.Parse(getNextData(file, '\t'));
                        DB_Item item = null;
                        try
                        {
                            item = Datas.OfType<DB_Item>().First(a => a.Identifier == itemId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Item - " + itemId + "\n")));
                        }

                        int abilityId = Int32.Parse(getNextData(file, '\t'));
                        DB_Ability ability = null;
                        try
                        {
                            ability = Datas.OfType<DB_Ability>().First(a => a.Identifier == abilityId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Ability - " + abilityId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (item.Abilities == null)
                            {
                                item.Abilities = new List<DB_Ability>();
                            }
                            item.Abilities.Add(ability);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ItemQuality:
                    while (!isEndOfFile(file))
                    {
                        DB_ItemQuality toAdd = new DB_ItemQuality();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ItemSet:
                    while (!isEndOfFile(file))
                    {
                        DB_ItemSet toAdd = new DB_ItemSet();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ItemSetEffects:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int itemSetId = Int32.Parse(getNextData(file, '\t'));
                        DB_ItemSet itemSet = null;
                        try
                        {
                            itemSet = Datas.OfType<DB_ItemSet>().First(a => a.Identifier == itemSetId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : ItemSet - " + itemSetId + "\n")));
                        }

                        int effectId = Int32.Parse(getNextData(file, '\t'));
                        DB_Effect effect = null;
                        try
                        {
                            effect = Datas.OfType<DB_Effect>().First(a => a.Identifier == effectId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Effect - " + effectId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (itemSet.Effects == null)
                            {
                                itemSet.Effects = new Dictionary<DB_Effect,byte>();
                            }
                            itemSet.Effects.Add(effect, byte.Parse(getNextData(file, '\t')));
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ItemSetItems:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int itemSetId = Int32.Parse(getNextData(file, '\t'));
                        DB_ItemSet itemSet = null;
                        try
                        {
                            itemSet = Datas.OfType<DB_ItemSet>().First(a => a.Identifier == itemSetId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : ItemSet - " + itemSetId + "\n")));
                        }

                        int itemId = Int32.Parse(getNextData(file, '\t'));
                        DB_Item item = null;
                        try
                        {
                            item = Datas.OfType<DB_Item>().First(a => a.Identifier == itemId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Item - " + itemId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (itemSet.Items == null)
                            {
                                itemSet.Items = new List<DB_Item>();
                            }
                            itemSet.Items.Add(item);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ItemStatistic:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int itemId = Int32.Parse(getNextData(file, '\t'));
                        DB_Item item = null;
                        try
                        {
                            item = Datas.OfType<DB_Item>().First(a => a.Identifier == itemId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Item - " + itemId + "\n")));
                        }

                        int statisticId = Int32.Parse(getNextData(file, '\t'));
                        DB_Statistic statistic = null;
                        try
                        {
                            statistic = Datas.OfType<DB_Statistic>().First(a => a.Identifier == statisticId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Statistic - " + statisticId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (item.Statistics == null)
                            {
                                item.Statistics = new Dictionary<DB_Statistic,int>();
                            }
                            item.Statistics.Add(statistic, byte.Parse(getNextData(file, '\t')));
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.NonPlayableCharacter:
                    while (!isEndOfFile(file))
                    {
                        bool isNew = false;

                        // Check existence
                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter toAdd = null;
                        try { if ((toAdd = this.Datas.OfType<DB_Vendor>().First<DB_Vendor>(c => c.Identifier == nonPlayableCharacterId)) != null) { } }
                        catch { }
                        // Checked

                        if (toAdd == null)
                        {
                            isNew = true;
                            toAdd = new DB_NonPlayableCharacter();
                        }
                        toAdd.Identifier = nonPlayableCharacterId;
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.LevelRange = new int[2] { Int32.Parse(getNextData(file, '\t')), Int32.Parse(getNextData(file, '\t')) };
                        toAdd.GivenExperience = Int32.Parse(getNextData(file, '\t'));

                        bool canOperate = true;
                        int friendshipLevelId = Int32.Parse(getNextData(file, '\t'));
                        DB_FriendshipLevel friendshipLevel = null;
                        try
                        {
                            friendshipLevel = Datas.OfType<DB_FriendshipLevel>().First(a => a.Identifier == friendshipLevelId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : FriendshipLevel - " + friendshipLevelId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.FriendshipLevel = friendshipLevel;
                        }

                        canOperate = true;
                        int raceId = Int32.Parse(getNextData(file, '\t'));
                        DB_CharacterRace race = null;
                        try
                        {
                            race = Datas.OfType<DB_CharacterRace>().First(a => a.Identifier == raceId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : CharacterRace - " + raceId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.Race = race;
                        }
                        toAdd.IsCaster = bool.Parse(Int32.Parse(getNextData(file, '\t')) == 1 ? Boolean.TrueString : Boolean.FalseString);

                        if (isNew)
                        {
                            Datas.Add(toAdd);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.NonPlayableCharacterAbility:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter nonPlayableCharacter = null;
                        try
                        {
                            nonPlayableCharacter = Datas.OfType<DB_NonPlayableCharacter>().First(a => a.Identifier == nonPlayableCharacterId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : NonPlayableCharacter - " + nonPlayableCharacterId + "\n")));
                        }

                        int abilityId = Int32.Parse(getNextData(file, '\t'));
                        DB_Ability ability = null;
                        try
                        {
                            ability = Datas.OfType<DB_Ability>().First(a => a.Identifier == abilityId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Ability - " + abilityId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (nonPlayableCharacter.Abilities == null)
                            {
                                nonPlayableCharacter.Abilities = new List<DB_Ability>();
                            }
                            nonPlayableCharacter.Abilities.Add(ability);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.NonPlayableCharacterItem:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter nonPlayableCharacter = null;
                        try
                        {
                            nonPlayableCharacter = Datas.OfType<DB_NonPlayableCharacter>().First(a => a.Identifier == nonPlayableCharacterId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : NonPlayableCharacter - " + nonPlayableCharacterId + "\n")));
                        }

                        int itemId = Int32.Parse(getNextData(file, '\t'));
                        DB_Item item = null;
                        try
                        {
                            item = Datas.OfType<DB_Item>().First(a => a.Identifier == itemId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Item - " + itemId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (nonPlayableCharacter.SpecificLootTable == null)
                            {
                                nonPlayableCharacter.SpecificLootTable = new Dictionary<DB_Item,float>();
                            }
                            nonPlayableCharacter.SpecificLootTable.Add(item, float.Parse(getNextData(file, '\t')));
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.NonPlayableCharacterIteration:
                    while (!isEndOfFile(file))
                    {
                        DB_NonPlayableCharacterIteration toAdd = new DB_NonPlayableCharacterIteration();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        
                        bool canOperate = true;
                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter nonPlayableCharacter = null;
                        try
                        {
                            nonPlayableCharacter = Datas.OfType<DB_NonPlayableCharacter>().First(a => a.Identifier == nonPlayableCharacterId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : NonPlayableCharacter - " + nonPlayableCharacterId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.NonPlayableCharacter = nonPlayableCharacter;
                        }

                        toAdd.Position = new Vector2(float.Parse(getNextData(file, '\t')), float.Parse(getNextData(file, '\t')));

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.NonPlayableCharacterPointBars:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter nonPlayableCharacter = null;
                        try
                        {
                            nonPlayableCharacter = Datas.OfType<DB_NonPlayableCharacter>().First(a => a.Identifier == nonPlayableCharacterId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : NonPlayableCharacter - " + nonPlayableCharacterId + "\n")));
                        }

                        int pointBarId = Int32.Parse(getNextData(file, '\t'));
                        DB_PointBar pointBar = null;
                        try
                        {
                            pointBar = Datas.OfType<DB_PointBar>().First(a => a.Identifier == pointBarId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : PointBar - " + pointBarId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (nonPlayableCharacter.PointBars == null)
                            {
                                nonPlayableCharacter.PointBars = new List<DB_PointBar>();
                            }
                            nonPlayableCharacter.PointBars.Add(pointBar);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.NonPlayableCharacterSprites:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;
                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter nonPlayableCharacter = null;
                        try
                        {
                            nonPlayableCharacter = Datas.OfType<DB_NonPlayableCharacter>().First(a => a.Identifier == nonPlayableCharacterId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : NonPlayableCharacter - " + nonPlayableCharacterId + "\n")));
                        }
                        if (canOperate)
                        {
                            if (nonPlayableCharacter.Sprites == null)
                            {
                                nonPlayableCharacter.Sprites = new List<Texture2D>();
                            }
                            String sprite = getNextData(file, '\t');
                            try { nonPlayableCharacter.Sprites.Add(Content.Load<Texture2D>(sprite)); }
                            catch
                            {
                                ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - Fichier non existant : " + sprite + "\n")));
                            }
                        }
                    }
                    break;
                case FileNameList.NonPlayableCharacterStatistic:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter nonPlayableCharacter = null;
                        try
                        {
                            nonPlayableCharacter = Datas.OfType<DB_NonPlayableCharacter>().First(a => a.Identifier == nonPlayableCharacterId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : NonPlayableCharacter - " + nonPlayableCharacterId + "\n")));
                        }

                        int statisticId = Int32.Parse(getNextData(file, '\t'));
                        DB_Statistic statistic = null;
                        try
                        {
                            statistic = Datas.OfType<DB_Statistic>().First(a => a.Identifier == statisticId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Statistic - " + statisticId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (nonPlayableCharacter.BaseStatistics == null)
                            {
                                nonPlayableCharacter.BaseStatistics = new Dictionary<DB_Statistic,KeyValuePair<float,float>>();
                            }
                            float value = float.Parse(getNextData(file, '\t'));
                            float multiplier = float.Parse(getNextData(file, '\t'));
                            nonPlayableCharacter.BaseStatistics.Add(statistic, new KeyValuePair<float, float>(value, multiplier));
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Objective:
                    while (!isEndOfFile(file))
                    {
                        DB_Objective toAdd = new DB_Objective();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Times = byte.Parse(getNextData(file, '\t'));

                        bool canOperate = true;
                        int typeId = Int32.Parse(getNextData(file, '\t'));
                        DB_ObjectiveType type = null;
                        try
                        {
                            type = Datas.OfType<DB_ObjectiveType>().First(a => a.Identifier == typeId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : ObjectiveType - " + typeId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.Type = type;
                        }

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ObjectiveTarget:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;
                        int objectiveId = Int32.Parse(getNextData(file, '\t'));
                        DB_Objective objective = null;
                        try
                        {
                            objective = Datas.OfType<DB_Objective>().First(a => a.Identifier == objectiveId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Objective - " + objectiveId + "\n")));
                        }

                        String table = "ProjetS5.DB_" + getNextData(file, '\t');
                        int targetId = Int32.Parse(getNextData(file, '\t'));
                        DatabaseTable target = null;
                        try
                        {
                            target = Datas.Where(e => e.GetType() == Type.GetType(table)).Single(e => e.Identifier == targetId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : " + table + " - " + targetId + "\n")));
                        }

                        if (canOperate)
                        {
                            objective.Object = target;
                        }
                        
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ObjectiveType:
                    while (!isEndOfFile(file))
                    {
                        DB_ObjectiveType toAdd = new DB_ObjectiveType();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Type = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.PointBar:
                    while (!isEndOfFile(file))
                    {
                        DB_PointBar toAdd = new DB_PointBar();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Quest:
                    while (!isEndOfFile(file))
                    {
                        DB_Quest toAdd = new DB_Quest();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.Description = getNextData(file, '\t');
                        toAdd.Level = byte.Parse(getNextData(file, '\t'));
                        toAdd.RequiredLevel = byte.Parse(getNextData(file, '\t'));

                        bool canOperate = true;
                        int rewardId = Int32.Parse(getNextData(file, '\t'));
                        DB_QuestReward reward = null;
                        try
                        {
                            reward = Datas.OfType<DB_QuestReward>().First(a => a.Identifier == rewardId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : QuestReward - " + rewardId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.Reward = reward;
                        }

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.QuestNonPlayableCharacterGive:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int questId = Int32.Parse(getNextData(file, '\t'));
                        DB_Quest quest = null;
                        try
                        {
                            quest = Datas.OfType<DB_Quest>().First(a => a.Identifier == questId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Quest - " + questId + "\n")));
                        }

                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter nonPlayableCharacter = null;
                        try
                        {
                            nonPlayableCharacter = Datas.OfType<DB_NonPlayableCharacter>().First(a => a.Identifier == nonPlayableCharacterId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : NonPlayableCharacter - " + nonPlayableCharacterId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (quest.Givers == null)
                            {
                                quest.Givers = new List<DB_NonPlayableCharacter>();
                            }
                            quest.Givers.Add(nonPlayableCharacter);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.QuestNonPlayableCharacterTurn:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int questId = Int32.Parse(getNextData(file, '\t'));
                        DB_Quest quest = null;
                        try
                        {
                            quest = Datas.OfType<DB_Quest>().First(a => a.Identifier == questId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Quest - " + questId + "\n")));
                        }

                        int nonPlayableCharacterId = Int32.Parse(getNextData(file, '\t'));
                        DB_NonPlayableCharacter nonPlayableCharacter = null;
                        try
                        {
                            nonPlayableCharacter = Datas.OfType<DB_NonPlayableCharacter>().First(a => a.Identifier == nonPlayableCharacterId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : NonPlayableCharacter - " + nonPlayableCharacterId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (quest.Turners == null)
                            {
                                quest.Turners = new List<DB_NonPlayableCharacter>();
                            }
                            quest.Turners.Add(nonPlayableCharacter);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.QuestObjectives:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int questId = Int32.Parse(getNextData(file, '\t'));
                        DB_Quest quest = null;
                        try
                        {
                            quest = Datas.OfType<DB_Quest>().First(a => a.Identifier == questId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Quest - " + questId + "\n")));
                        }

                        int objectiveId = Int32.Parse(getNextData(file, '\t'));
                        DB_Objective objective = null;
                        try
                        {
                            objective = Datas.OfType<DB_Objective>().First(a => a.Identifier == objectiveId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Objective - " + objectiveId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (quest.Objectives == null)
                            {
                                quest.Objectives = new List<DB_Objective>();
                            }
                            quest.Objectives.Add(objective);
                        }
                        
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.QuestQuest:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int questId = Int32.Parse(getNextData(file, '\t'));
                        DB_Quest quest = null;
                        try
                        {
                            quest = Datas.OfType<DB_Quest>().First(a => a.Identifier == questId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Quest - " + questId + "\n")));
                        }

                        int requiredQuestId = Int32.Parse(getNextData(file, '\t'));
                        DB_Quest requiredQuest = null;
                        try
                        {
                            quest = Datas.OfType<DB_Quest>().First(a => a.Identifier == questId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Quest - " + requiredQuestId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (quest.QuestsRequired == null)
                            {
                                quest.QuestsRequired = new List<DB_Quest>();
                            }
                            quest.QuestsRequired.Add(requiredQuest);
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.QuestReward:
                    while (!isEndOfFile(file))
                    {
                        DB_QuestReward toAdd = new DB_QuestReward();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Experience = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Gold = Int32.Parse(getNextData(file, '\t'));

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.QuestRewardItems:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int questRewardId = Int32.Parse(getNextData(file, '\t'));
                        DB_QuestReward questReward = null;
                        try
                        {
                            questReward = Datas.OfType<DB_QuestReward>().First(a => a.Identifier == questRewardId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : QuestReward - " + questRewardId + "\n")));
                        }

                        int itemId = Int32.Parse(getNextData(file, '\t'));
                        DB_Item item = null;
                        try
                        {
                            item = Datas.OfType<DB_Item>().First(a => a.Identifier == itemId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Item - " + itemId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (questReward.Items == null)
                            {
                                questReward.Items = new Dictionary<DB_Item, byte>();
                            }
                            questReward.Items.Add(item, byte.Parse(getNextData(file, '\t')));
                        }
                        
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.ReplaceString:
                    while (!isEndOfFile(file))
                    {
                        DB_ReplaceString toAdd = new DB_ReplaceString();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.ToReplace = getNextData(file, '\t');
                        toAdd.ReplaceBy_Description = getNextData(file, '\t');
                        
                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Restriction:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;
                        DB_Restriction toAdd = new DB_Restriction();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));

                        String table = "ProjetS5.DB_" + getNextData(file, '\t');
                        int targetId = Int32.Parse(getNextData(file, '\t'));
                        DatabaseTable target = null;
                        try
                        {
                            target = Datas.Where(e => e.GetType() == Type.GetType(table)).Single(e => e.Identifier == targetId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : " + table + " - " + targetId + "\n")));
                        }

                        if (canOperate)
                        {
                            toAdd.Restricted = target;
                        }

                        Datas.Add(toAdd);

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.RestrictionRestrictors:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;
                        int restrictionId = Int32.Parse(getNextData(file, '\t'));
                        DB_Restriction restriction = null;
                        try
                        {
                            restriction = Datas.OfType<DB_Restriction>().First(a => a.Identifier == restrictionId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Restriction - " + restrictionId + "\n")));
                        }

                        String table = "ProjetS5.DB_" + getNextData(file, '\t');
                        int targetId = Int32.Parse(getNextData(file, '\t'));
                        DatabaseTable target = null;
                        try
                        {
                            target = Datas.Where(e => e.GetType() == Type.GetType(table)).Single(e => e.Identifier == targetId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : " + table + " - " + targetId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (restriction.Restrictings == null)
                            {
                                restriction.Restrictings = new List<DatabaseTable>();
                            }
                            restriction.Restrictings.Add(target);
                        }
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Statistic:
                    while (!isEndOfFile(file))
                    {
                        DB_Statistic toAdd = new DB_Statistic();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.Description = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Talent:
                    while (!isEndOfFile(file))
                    {
                        DB_Talent toAdd = new DB_Talent();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');
                        toAdd.Description = getNextData(file, '\t');
                        toAdd.Rank = byte.Parse(getNextData(file, '\t'));

                        bool canOperate = true;
                        int requiredTalentId = Int32.Parse(getNextData(file, '\t'));
                        DB_Talent requiredTalent = null;
                        try
                        {
                            requiredTalent = Datas.OfType<DB_Talent>().First(a => a.Identifier == requiredTalentId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Talent - " + requiredTalentId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.TalentRequired = requiredTalent;
                        }

                        canOperate = true;
                        int abilityId = Int32.Parse(getNextData(file, '\t'));
                        DB_Ability ability = null;
                        try
                        {
                            ability = Datas.OfType<DB_Ability>().First(a => a.Identifier == abilityId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Ability - " + abilityId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.Ability = ability;
                        }

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.TalentEffects:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int talentId = Int32.Parse(getNextData(file, '\t'));
                        DB_Talent talent = null;
                        try
                        {
                            talent = Datas.OfType<DB_Talent>().First(a => a.Identifier == talentId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Talent - " + talentId + "\n")));
                        }

                        int effectId = Int32.Parse(getNextData(file, '\t'));
                        DB_Effect effect = null;
                        try
                        {
                            effect = Datas.OfType<DB_Effect>().First(a => a.Identifier == effectId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Effect - " + effectId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (talent.Effects == null)
                            {
                                talent.Effects = new List<DB_Effect>();
                            }
                            talent.Effects.Add(effect);
                        }
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.TalentTree:
                    while (!isEndOfFile(file))
                    {
                        DB_TalentTree toAdd = new DB_TalentTree();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Name = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.TalentTreeTalents:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int talentTreeId = Int32.Parse(getNextData(file, '\t'));
                        DB_TalentTree talentTree = null;
                        try
                        {
                            talentTree = Datas.OfType<DB_TalentTree>().First(a => a.Identifier == talentTreeId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : TalentTree - " + talentTreeId + "\n")));
                        }

                        int talentId = Int32.Parse(getNextData(file, '\t'));
                        DB_Talent talent = null;
                        try
                        {
                            talent = Datas.OfType<DB_Talent>().First(a => a.Identifier == talentId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Talent - " + talentId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (talentTree.Talents == null)
                            {
                                talentTree.Talents = new List<DB_Talent>();
                            }
                            talentTree.Talents.Add(talent);
                        }
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.TickType:
                    while (!isEndOfFile(file))
                    {
                        DB_TickType toAdd = new DB_TickType();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Type = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Vendor:
                    while (!isEndOfFile(file))
                    {
                        DB_Vendor toAdd = new DB_Vendor();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.IsReparator = (Int32.Parse(getNextData(file, '\t')) == 1 ? true : false);

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.VendorSell:
                    while (!isEndOfFile(file))
                    {
                        bool canOperate = true;

                        int vendorId = Int32.Parse(getNextData(file, '\t'));
                        DB_Vendor vendor = null;
                        try
                        {
                            vendor = Datas.OfType<DB_Vendor>().First(a => a.Identifier == vendorId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Vendor - " + vendorId + "\n")));
                        }

                        int itemId = Int32.Parse(getNextData(file, '\t'));
                        DB_Item item = null;
                        try
                        {
                            item = Datas.OfType<DB_Item>().First(a => a.Identifier == itemId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : Item - " + itemId + "\n")));
                        }

                        if (canOperate)
                        {
                            if (vendor.Sell == null)
                            {
                                vendor.Sell = new Dictionary<DB_Item,int>();
                            }
                            vendor.Sell.Add(item, Int32.Parse(getNextData(file, '\t')));
                        }

                        goToNextLine(file);
                    }
                    break;
                case FileNameList.Weapon:
                    while (!isEndOfFile(file))
                    {
                        DB_Weapon toAdd = new DB_Weapon();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.AttackRange = new int[2] {Int32.Parse(getNextData(file, '\t')), Int32.Parse(getNextData(file, '\t'))};
                        toAdd.Cooldown = new TimeSpan(0, 0, 0, 0, Int32.Parse(getNextData(file, '\t')));
                        toAdd.IsOneHanded = (Int32.Parse(getNextData(file, '\t')) == 1 ? true : false);

                        bool canOperate = true;
                        int weaponTypeId = Int32.Parse(getNextData(file, '\t'));
                        DB_WeaponType weaponType = null;
                        try
                        {
                            weaponType = Datas.OfType<DB_WeaponType>().First(a => a.Identifier == weaponTypeId);
                        }
                        catch
                        {
                            canOperate = false;
                            ErrorLog.Export(errorLogName, new System.Text.UTF8Encoding().GetBytes(("Erreur - L'entrée n'existe pas (table - id) : WeaponType - " + weaponTypeId + "\n")));
                        }
                        if (canOperate)
                        {
                            toAdd.WeaponType = weaponType;
                        }

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
                case FileNameList.WeaponType:
                    while (!isEndOfFile(file))
                    {
                        DB_WeaponType toAdd = new DB_WeaponType();
                        toAdd.Identifier = Int32.Parse(getNextData(file, '\t'));
                        toAdd.Type = getNextData(file, '\t');

                        Datas.Add(toAdd);
                        goToNextLine(file);
                    }
                    break;
            }
        }
    }
}
